package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;

import system.LoginContext;

import client.XMLSend;

import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.form.GSProviderAddForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.business.GSProviderProcessor;

import framework.core.Config;
import framework.core.DateUtil;
import framework.core.ILoginContext;
import framework.core.SrvContext;

/**
 * 添加供应商
 */
public class GSProviderAddAction extends GSBaseAction {
    public String execute(GSBaseForm aForm) throws Exception {
        GSProviderAddForm tForm = (GSProviderAddForm) aForm;
        SrvContext context = SrvContext.getContext(aForm.getRequest());
        LoginContext loginInfo = LoginContext.get(context);
        GSProviderBean tGSProviderBean = new GSProviderBean();
        try {
            
            BeanUtils.copyProperties(tGSProviderBean, tForm);
            // 不是交易平台导入
            tGSProviderBean.setGs_is_tradeflat_provider("1");
            // 添加供应商
            tGSProviderBean.setLast_update_user_id(loginInfo.getLoginUserId());
            tGSProviderBean.setLast_update_user(loginInfo.getLoginUserName());
            tGSProviderBean.setLast_update_time(new DateUtil().getFullString());
            tGSProviderBean.setCreate_user_id(loginInfo.getLoginUserId());
            tGSProviderBean.setCreate_user(loginInfo.getLoginUserName());
            tGSProviderBean.setCreate_time(new DateUtil().getFullString());
            
            GSProviderProcessor tProviderProcessor = new GSProviderProcessor();
            tProviderProcessor.addProvider(tGSProviderBean, tForm.getIsAudit(), loginInfo);
            
        } catch (GSException err) {
            if (err.getCode() != null) {
                HashMap tMap = new HashMap();
                tMap.put("err", err.getMessage());
                aForm.getRequest().setAttribute("_error", tMap);
                aForm.getRequest().setAttribute("message", err.getMessage());
                aForm.getRequest().setAttribute("exception_code", err.getCode());
                aForm.getRequest().setAttribute("requery", "true");
                aForm.getRequest().setAttribute("action", "A400101.do");
                throw err;
            }
        }
        
      //荣爱接口通讯
        // 设置xml参数
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitProvider";
            String dataName = "Provider";
            // 传输xml to 荣爱
            XMLSend.getInstance().submitProviderSendXML(actionName, dataName, tGSProviderBean);

        }
        aForm.getRequest().setAttribute("message", "新增供应商成功!");
        aForm.getRequest().setAttribute("requery", "true");

        // 取得所有国家
        ArrayList tList = GSBaseManager.getAllCountry();
        aForm.getRequest().setAttribute("country", tList);
        // 取得所有省
        tList = GSBaseManager.getAllProvince();
        aForm.getRequest().setAttribute("province", tList);
        // 取得所有地区
        tList = GSBaseManager.getAllDistinct();
        aForm.getRequest().setAttribute("district", tList);
        // 取得公司性质
        String[][] tStr = GSBaseManager.getAllCorpProperty();
        aForm.getRequest().setAttribute("corpproperty", tStr);
        return GSBaseConstants.FORWARD_SUCCESS;
    }
}
